import UIAbility from '@ohos.app.ability.UIAbility';
import hilog from '@ohos.hilog';
import window from '@ohos.window';
import formProvider from '@ohos.app.form.formProvider';
import formBindingData from '@ohos.app.form.formBindingData';
import formInfo from '@ohos.app.form.formInfo';

let currentWindowStage = null;

export default class EntryAbility extends UIAbility {
  onCreate(want, launchParam) {
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');

    // 解析传递来的参数
    this.handleWant(want)

    // 传递参数在want对象中
    console.log("testTag", "onCreate------want:" + JSON.stringify(want))
    console.log("testTag", "onCreate------launchParam:" + JSON.stringify(launchParam))

  }

  onNewWant(want, launchParams) {
    // 传递参数在want对象中
    console.log("testTag", "onNewWant------want:" + JSON.stringify(want))
    console.log("testTag", "onNewWant------launchParam:" + JSON.stringify(launchParams))

    // 处理传递来的参数
    this.handleWant(want)
  }

  onDestroy() {
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy');
  }

  onWindowStageCreate(windowStage: window.WindowStage) {
    // Main window is created, set main page for this ability
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');

    // 缓存windowStage，用于后续调用使用
    if (currentWindowStage === null) {
      currentWindowStage = windowStage
    }

    // 加载指定页面
    windowStage.loadContent('pages/Index', (err, data) => {
      if (err.code) {
        hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
        return;
      }
      hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? '');
    });
  }

  onWindowStageDestroy() {
    // Main window is destroyed, release UI related resources
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy');
  }

  onForeground() {
    // Ability has brought to foreground
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground');
  }

  onBackground() {
    // Ability has back to background
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground');
  }
  /**
   * 处理want参数
   * @param want
   */
  handleWant(want) {
    // try {
    //   // 监听call事件所需的方法
    //   this.callee.on(MSG_SEND_METHOD, FunACall);
    // } catch (error) {
    //   console.log(`${MSG_SEND_METHOD} register failed with error ${JSON.stringify(error)}`)
    // }

    // 服务卡片参数
    if (want.parameters[formInfo.FormParam.IDENTITY_KEY] !== undefined) {
      // 来自服务卡片的formId
      let formId = want.parameters[formInfo.FormParam.IDENTITY_KEY];
      // 解析传递来的参数
      if (want.parameters.params !== undefined) {
        let params = JSON.parse(want.parameters.params)
        let detail = params['detail']

        console.log("testTag", "onCreate-----params:" + JSON.stringify(params))

        // 解析特殊参数（服务卡片传递来的特殊参数）
        if (detail && detail === "RouterFromCard") {
          let formData = {
            title: "呱呱卡片",
            message: "我现在可以接受呱呱消息展示啦，这个消息来自EntryAbility"
          }
          let formInfo = formBindingData.createFormBindingData(formData)
          // 刷新卡片信息
          formProvider.updateForm(formId, formInfo).then((data) => {
            console.info('testTag', 'FormAbility updateForm success.' + JSON.stringify(data));
          }).catch((error) => {
            console.error('testTag', 'FormAbility updateForm failed: ' + JSON.stringify(error));
          })
        }
      }
    }


  }
}
